/*
GANESHA - Reasoning on critical infrastructures affected by emergency situations
Copyright (c) 2010-2011, Information Eng. Research Unit - Univ. of Alcala
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package es.uah.cc.ie.ganesha;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

/**
 * Encapsulates GANESHA configuration.
 * @author ieru
 */
public class GANESHAConfiguration implements Configuration {

    protected HashMap _params;
    protected HashMap _ontologies;

    /**
     * Constructor. Takes the configuration from a XML file.
     * @param configurationFile Path to the configuration XML file.
     * @throws JDOMException
     * @throws IOException
     */
    public GANESHAConfiguration(String configurationFile) throws JDOMException, IOException {
        this(new FileInputStream(new File(configurationFile)));
    }

    /**
     * Constructor. Takes the configuration from a XML file.
     * @param configurationFile InputStream to the configuration XML file.
     * @throws JDOMException
     * @throws IOException
     */
    public GANESHAConfiguration(InputStream configurationFile) throws JDOMException, IOException {
        this._params = new HashMap();
        this._ontologies = new HashMap();

        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(configurationFile);
        Element root = doc.getRootElement();
        Namespace ns = root.getNamespace();

        List<Element> params = root.getChild("ganesha", ns).getChildren("param", ns);

        for (Element param : params) {
            String key = param.getChild("key", ns).getTextTrim();
            String value = param.getChild("value", ns).getTextTrim();

            this._params.put(key, value);
        }

        List<Element> ontologies = root.getChild("ganesha", ns).getChildren("ontology", ns);

        for (Element ont : ontologies) {
            String file = ont.getChild("file", ns).getTextTrim();
            String uri = ont.getChild("uri", ns).getTextTrim();

            _ontologies.put(uri, file);
        }
    }

    /**
     * Constructor. Takes the configuration from a HashMap.
     * @param params Parameters.
     */
    public GANESHAConfiguration(HashMap params) {
        _params = new HashMap();

        for (Entry entry : (Set<Entry>) params.entrySet()) {
            if (entry.getKey().toString().startsWith("GANESHA")) {
                _params.put(entry.getKey(), entry.getValue());
            }
        }

        _ontologies = (HashMap) _params.get("GANESHA_ONTOLOGIES");
    }

    /**
     * Returns the parameters.
     * @return Parameters.
     */
    public HashMap getParams() {
        return _params;
    }

    /**
     * Returns the ontologies.
     * @return Ontologies.
     */
    public HashMap getOntologies() {
        return _ontologies;
    }
}
